package com.yiyukongjian.house;

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class HouseInfoAdd extends Activity implements OnClickListener {

	private Button btnSave;
	//房屋信息
	private EditText etHouseAddress;
	private EditText etHostName;
	private EditText etHostIDCard;
	private EditText etHostTel;
	//承租人信息
	private EditText etRentName;
	private EditText etRentIDCard;
	private EditText etRentTel;
	private EditText etRentStartDate;
	private EditText etRentEndDate;
	//同住人信息
	private EditText etRenterName;
	private EditText etRenterIDCard;
	private EditText etRenterTel;
	
	//同住人信息添加button
	private Button btnAddRenter;
	private LinearLayout renterAddLayout;
	
	
    static final int START_DATE_DIALOG_ID = 0;
    static final int END_DATE_DIALOG_ID = 1;
    // date and time
    private int mYear;
    private int mMonth;
    private int mDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_add);
		
		findView();
		btnSave.setOnClickListener(this);
		etRentStartDate.setOnClickListener(this);
		etRentEndDate.setOnClickListener(this);
		
		btnAddRenter.setOnClickListener(this);
		
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
	}
	
	private void findView()
	{
		btnSave = (Button)findViewById(R.id.btnSave);
		etHouseAddress = (EditText)findViewById(R.id.et_houseAddress);
		etHostName = (EditText)findViewById(R.id.et_hostName);
		etHostIDCard = (EditText)findViewById(R.id.et_hostIDCard);
		etHostTel = (EditText)findViewById(R.id.et_hostTel);
		
		etRentName = (EditText)findViewById(R.id.et_rentName);
		etRentIDCard = (EditText)findViewById(R.id.et_rentIDCard);
		etRentTel = (EditText)findViewById(R.id.et_rentTel);
		etRentStartDate = (EditText)findViewById(R.id.et_rentStartDate);
		etRentEndDate = (EditText)findViewById(R.id.et_rentEndDate);
		
		etRenterName = (EditText)findViewById(R.id.et_renterName);
		etRenterIDCard = (EditText)findViewById(R.id.et_renterIDCard);
		etRenterTel = (EditText)findViewById(R.id.et_renterTel);
		
		btnAddRenter = (Button)findViewById(R.id.renterAdd);
		renterAddLayout = (LinearLayout)findViewById(R.id.addLayout);
		
		
		
	}
	
	private void doReset()
	{
		etHouseAddress.setText("");
		etHostName.setText("");
		etHostIDCard.setText("");
		etHostTel.setText("");
		
		etRentName.setText("");
		etRentIDCard.setText("");
		etRentTel.setText("");
		etRentStartDate.setText("");
		etRentEndDate.setText("");
		
		etRenterName.setText("");
		etRenterIDCard.setText("");
		etRenterTel.setText("");
	}
	private void doSave()
	{
		// Reset errors.
		etHouseAddress.setError(null);
		etHostName.setError(null);
		etHostIDCard.setError(null);
		etHostTel.setError(null);
		
		etRentName.setError(null);
		etRentIDCard.setError(null);
		etRentTel.setError(null);
		etRentStartDate.setError(null);
		etRentEndDate.setError(null);
		
		etRenterName.setError(null);
		etRenterIDCard.setError(null);
		etRenterTel.setError(null);
		
		boolean cancel = false;
		View focusView = null;
		
		String hostAddress = etHouseAddress.getText().toString();
		if(TextUtils.isEmpty(hostAddress) )
		{
			etHouseAddress.setError(getString(R.string.error_field_required));
			focusView = etHouseAddress;
			cancel = true;
		}
		
		String hostName = etHostName.getText().toString();
		if(TextUtils.isEmpty(hostName) )
		{
			etHostName.setError(getString(R.string.error_field_required));
			focusView = etHostName;
			cancel = true;
		}
		
		String hostIDCard = etHostIDCard.getText().toString();
		if(TextUtils.isEmpty(hostIDCard) )
		{
			etHostIDCard.setError(getString(R.string.error_field_required));
			focusView = etHostIDCard;
			cancel = true;
		}
		
		String hostTel = etHostTel.getText().toString();
		if(TextUtils.isEmpty(hostTel) )
		{
			etHostTel.setError(getString(R.string.error_field_required));
			focusView = etHostTel;
			cancel = true;
		}
		
		
		String rentName = etRentName.getText().toString();
		if(TextUtils.isEmpty(rentName) )
		{
			etRentName.setError(getString(R.string.error_field_required));
			focusView = etRentName;
			cancel = true;
		}
		
		String rentIDCard = etRentIDCard.getText().toString();
		if(TextUtils.isEmpty(rentIDCard) )
		{
			etRentIDCard.setError(getString(R.string.error_field_required));
			focusView = etRentIDCard;
			cancel = true;
		}
		
		String rentTel = etRentTel.getText().toString();
		if(TextUtils.isEmpty(rentTel) )
		{
			etRentTel.setError(getString(R.string.error_field_required));
			focusView = etRentTel;
			cancel = true;
		}
		
		String rentStartDate = etRentStartDate.getText().toString();
		if(TextUtils.isEmpty(rentStartDate) )
		{
			etRentStartDate.setError(getString(R.string.error_field_required));
			focusView = etRentStartDate;
			cancel = true;
		}
		
		String rentEndDate = etRentEndDate.getText().toString();
		if(TextUtils.isEmpty(rentEndDate) )
		{
			etRentEndDate.setError(getString(R.string.error_field_required));
			focusView = etRentEndDate;
			cancel = true;
		}	
		
//		String renterName = etRenterName.getText().toString();
//		if(TextUtils.isEmpty(renterName) )
//		{
//			etRenterName.setError(getString(R.string.error_field_required));
//			focusView = etRenterName;
//			cancel = true;
//		}
//		
//		String renterIDCard = etRenterIDCard.getText().toString();
//		if(TextUtils.isEmpty(renterIDCard) )
//		{
//			etRenterIDCard.setError(getString(R.string.error_field_required));
//			focusView = etRenterIDCard;
//			cancel = true;
//		}
//		
//		String renterTel = etRenterTel.getText().toString();
//		if(TextUtils.isEmpty(renterTel) )
//		{
//			etRenterTel.setError(getString(R.string.error_field_required));
//			focusView = etRenterTel;
//			cancel = true;
//		}
		
		final int count = renterAddLayout.getChildCount();
		String message = "";
		for(int index = 0; index < count; index ++)
		{
			LinearLayout layout = (LinearLayout) renterAddLayout.getChildAt(index);
			EditText text1 = (EditText) layout.findViewById(R.id.et_itemRenterName);
			EditText text2 = (EditText) layout.findViewById(R.id.et_itemRenterIDCard);
			EditText text3 = (EditText) layout.findViewById(R.id.et_itemRenterTel);
			message += String.valueOf(index) + ":" + text1.getText() + text2.getText() + text3.getText() + "\n";
		}
		
		
		if (cancel) 
		{
			focusView.requestFocus();
			Toast.makeText(this, "录入错误，请检查！", Toast.LENGTH_SHORT).show();
		} 
		else 
		{
			//do save to websercie
			
			Toast.makeText(this, "保存成功！"+ message, Toast.LENGTH_SHORT).show();
			doReset();
		}

	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
			case R.id.btnSave:
				doSave();
				break;
			case R.id.et_rentStartDate:
				showDialog(START_DATE_DIALOG_ID);
				break;
			case R.id.et_rentEndDate:
				showDialog(END_DATE_DIALOG_ID);
				break;
			case R.id.renterAdd:
				addRenterView();
				break;
			default:
				break;
		}		
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
        switch (id) {
        case START_DATE_DIALOG_ID:
            return new DatePickerDialog(this,
            		mStartDateSetListener,
                        mYear, mMonth, mDay);
        case END_DATE_DIALOG_ID:
            return new DatePickerDialog(this,
            		mEndDateSetListener,
                        mYear, mMonth, mDay);
    }
    return null;
	}
    private DatePickerDialog.OnDateSetListener mStartDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay(START_DATE_DIALOG_ID);
                }
            };
    private DatePickerDialog.OnDateSetListener mEndDateSetListener =
                    new DatePickerDialog.OnDateSetListener() {

                        public void onDateSet(DatePicker view, int year, int monthOfYear,
                                int dayOfMonth) {
                            mYear = year;
                            mMonth = monthOfYear;
                            mDay = dayOfMonth;
                            updateDisplay(END_DATE_DIALOG_ID);
                        }
                    };
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
    	((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
    }    

    private void updateDisplay(int id) {
    	
        switch (id) {
        case START_DATE_DIALOG_ID:
        	etRentStartDate.setText(
                    new StringBuilder()
                            // Month is 0 based so add 1
                            .append(mMonth + 1).append("-")
                            .append(mDay).append("-")
                            .append(mYear).append(" "));
        	break;
        case END_DATE_DIALOG_ID:
        	etRentEndDate.setText(
                    new StringBuilder()
                            // Month is 0 based so add 1
                            .append(mMonth + 1).append("-")
                            .append(mDay).append("-")
                            .append(mYear).append(" "));
        	break;
    }

    }
    
    private void addRenterView()
    {
    	final int index = renterAddLayout.getChildCount();
    	LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.showitem, null);
    	Button btnDelete = (Button)layout.findViewById(R.id.deleteRenter);
    	btnDelete.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {			
				renterAddLayout.removeViewAt(index);
			}
    		
    	});
    	renterAddLayout.addView(layout, index);    	
    }
}
